package com.zang.tree;

/**
 * @author ZVerify
 * @since 2022/11/01 18:51
 * @see <a href="https://leetcode.cn/problems/minimum-absolute-difference-in-bst/submissions/379110665/">...</a>
 **/
public class 二叉搜索树的最小绝对差 {

    int result = Integer.MAX_VALUE;
    int pre = -1;
    public int getMinimumDifference(TreeNode root) {

        dfs(root);
        return result;
    }

    private void dfs(TreeNode root) {
        if (root==null) {
            return ;
        }
        dfs(root.left);

        // 初始化根节点
        if (pre == -1) {
            pre = root.val;
        }else {
            result = Math.min(Math.abs(root.val-pre),result);
            // 更新当前节点给下一节点计算
            pre = root.val;
        }
        dfs(root.right);
    }
}
